package com.lijiajian.controller;

import com.lijiajian.Service.PermissionService;
import com.lijiajian.Service.UserService;
import com.lijiajian.base.BaseController;
import com.lijiajian.pojo.User;
import com.lijiajian.utils.LoginUserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController extends BaseController {
    @Resource
    private UserService userService;
    @Resource
    private PermissionService permissionService;
    @GetMapping("/index")
    public String index(Model model, HttpServletRequest request) {
        return "index";
    }

    /**
     * 系统欢迎页
     * @return
     */
    @GetMapping("/welcome")
    public String welcome() {
        return "welcome";
    }

    /**
     * 后端管理主页面
     * @return
     */
    @GetMapping({"/","/main"})
    public String main(HttpServletRequest request) {
        //获取cookie中的用户ID
        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        User user = (User) userService.selectByPrimaryKey(userId);
        //保存在session中
        request.getSession().setAttribute("user",user);
        List<String> aclValues = permissionService.queryUserHasRoleHasPermissionByUserId(userId);
        request.getSession().setAttribute("permissions",aclValues);
        return "main";
    }

}
